package com.ruoyi.product.service.impl;

import java.util.*;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.product.domain.ProductDetail;
import com.ruoyi.product.mapper.ProductDetailMapper;
import com.ruoyi.utils.TimestampIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.ProductOrderMapper;
import com.ruoyi.product.domain.ProductOrder;
import com.ruoyi.product.service.IProductOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 商品订单Service业务层处理
 * 
 * @author guoguoa
 * @date 2025-01-07
 */
@Service
public class ProductOrderServiceImpl implements IProductOrderService 
{
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Resource
    private RedisCache redisCache;
    /**
     * 查询商品订单
     * 
     * @param id 商品订单主键
     * @return 商品订单
     */
    @Override
    public ProductOrder selectProductOrderById(Long id)
    {
        return productOrderMapper.selectProductOrderById(id);
    }

    /**
     * 查询商品订单列表
     * 
     * @param productOrder 商品订单
     * @return 商品订单
     */
    @Override
    public List<ProductOrder> selectProductOrderList(ProductOrder productOrder)
    {
        return productOrderMapper.selectProductOrderList(productOrder);
    }

    /**
     * 新增商品订单
     * 
     * @param productOrder 商品订单
     * @return 结果
     */
    @Override
    public ProductOrder insertProductOrder(ProductOrder productOrder)
    {
        try {
            productOrder.setId(TimestampIdGenerator.nextPkId());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        int result = productOrderMapper.insertProductOrder(productOrder);
        if (result > 0) {
            Integer productId = productOrder.getProductId();
            String cacheKey = "product_order_count:" + productId;
            redisCache.deleteObject(cacheKey);
            // 重新查询该商品的所有订单数量
            ProductOrder params = new ProductOrder();
            params.setProductId(productId);
            List<ProductOrder> updatedOrderList = productOrderMapper.selectProductOrderList(params);
            Map<Integer, Integer> updatedProductOrderCountMap = new HashMap<>();
            for (ProductOrder updatedOrder : updatedOrderList) {
                Integer updatedProductId = updatedOrder.getProductId();
                Integer updatedQuantity = updatedOrder.getProductNumber();
                updatedProductOrderCountMap.put(updatedProductId, updatedProductOrderCountMap.getOrDefault(updatedProductId, 0) + updatedQuantity);
            }
            Integer updatedOrderCount = updatedProductOrderCountMap.getOrDefault(productId, 0);

            // 将更新后的订单数重新存入 Redis 缓存
            redisCache.setCacheObject(cacheKey, updatedOrderCount);

            // 将商品详情表中当前的销售总量更新为最新的订单数量
            ProductDetail productDetail = productDetailMapper.selectProductDetailById(productId);
            if (productDetail!= null) {
                productDetail.setSalesQuantity(updatedOrderCount);
                productDetailMapper.updateProductDetail(productDetail);
            }
        }
        return productOrder;
    }

    /**
     * 修改商品订单
     * 
     * @param productOrder 商品订单
     * @return 结果
     */
    @Override
    public int updateProductOrder(ProductOrder productOrder)
    {
        return productOrderMapper.updateProductOrder(productOrder);
    }

    /**
     * 批量删除商品订单
     * 
     * @param ids 需要删除的商品订单主键
     * @return 结果
     */
    @Override
    public int deleteProductOrderByIds(Long[] ids)
    {
        return productOrderMapper.deleteProductOrderByIds(ids);
    }

    /**
     * 删除商品订单信息
     * 
     * @param id 商品订单主键
     * @return 结果
     */
    @Override
    public int deleteProductOrderById(Long id)
    {
        return productOrderMapper.deleteProductOrderById(id);
    }
}
